/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "ti_msp_dl_config.h"
#include "Variables.h"


//---------main valiables begin-----------------//
char buffer[20];                    //显示角度的buffer
char cmd;                           //第几问
void Vari_Init()
{
    Encoder_Count_Left=0;          //位移值左轮
    Encoder_Count_Right=0;         //位移值右轮
    Encoder_Count_Left_temp=0;     //位移值左轮--用于速度计算
    Encoder_Count_Right_temp=0;    //位移值右轮--用于速度计算
}
uint32_t push_time=0;
uint32_t mode=0;
int Get_Cmd(char *cmd)
{
    if (!DL_GPIO_readPins(KEY_PORT,KEY_PIN_12_PIN)) // 按钮按下
    {
        delay_ms1(10);
        if (!DL_GPIO_readPins(KEY_PORT,KEY_PIN_12_PIN)) // 按钮按下
        {
            while (!DL_GPIO_readPins(KEY_PORT,KEY_PIN_12_PIN)) 
            {
                push_time++;
                delay_ms(10);
                if (push_time >= 100)return 1;
            }
            
            OLED_ShowNum(70,6,mode,1,16);
            
            switch (mode) 
            {
                case 1 :
                    (*cmd) = 'A';
                break;

                case 2 :
                    (*cmd) = 'B';
                break;

                case 3 :
                    (*cmd) = 'C';
                break;

                case 4 :
                    (*cmd) = 'D';
                break;

                case 5 :
                    (*cmd) = 'E';
                break;

                case 6 :
                    (*cmd) = 'F';
                break;

            }         
            mode++;
            if (mode >= 7)mode = 1;
            
            push_time = 0;
        }
    }
    return 0;
}

//---------main valiables end-----------------//

int main(void)
{
    SYSCFG_DL_init();
    
    PID_Init(&Speed_Left,10,2,0.5,500,1000);        //左轮速度环PID初始化
    PID_Init(&Speed_Right,10,2,0.5,500,1000);       //右轮速度环PID初始化
    PID_Init(&Distance_Left,1,0.001,1.5,500,1000);  //左轮位移环PID初始化
    PID_Init(&Distance_Right,1,0.001,1.5,500,1000); //右轮位移环PID初始化
    PID_Init(&Angle,-14,-0.2,-35,500,1000);          //角度环PID初始化
    PID_Init(&Gray,35,0,7,500,1000);                //灰度环PID初始化
    JY901S_RESET_YAW();                             //陀螺仪YAW角度归零
    OLED_Init();                                    //OLED初始化
    OLED_Clear();                                   //OLED清屏

    OLED_ShowString(0,0,"lx:",16);
    OLED_ShowString(0,2,"rx:",16);
    OLED_ShowString(0,4,"lv:",16);
    OLED_ShowString(0,6,"rv:",16);
    delay_ms1(200);
    Vari_Init();                               //变量初始化
    while (1) 
    {
        if (Get_Cmd(&cmd)) break;
    }
    OLED_ShowNum(70,6,9,1,16);
    delay_ms1(500);
    delay_ms1(500);
    delay_ms1(500);
    delay_ms1(500);
    NVIC_EnableIRQ(GPIO_MULTIPLE_GPIOB_INT_IRQN);   //编码器外部中断开启
    DL_TimerG_startCounter(MOTOR_PWM_INST);         //PWM定时器TIMA1开启
    DL_TimerG_startCounter(TIMER_0_INST);           //定时器TIMA0开启
    NVIC_EnableIRQ(UART_JY901S_INST_INT_IRQN);      //开陀螺仪接收中断
    NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);          //定时器TIMA0开中断

    while (1) 
    {   sprintf(buffer, "%.2f", yaw_angle);
        OLED_ShowString(70, 0, buffer, 16);
        OLED_ShowNum(25,0,(uint32_t)Encoder_Count_Left,5,16);   
        OLED_ShowNum(25,2,(uint32_t)Encoder_Count_Right,5,16);  OLED_ShowNum(70,2,(uint32_t)-Angle.output,5,16);
        OLED_ShowNum(25,4,(uint32_t)Left_speed,5,16);           OLED_ShowNum(70,4,(uint32_t)-Angle.integral,5,16);
        OLED_ShowNum(25,6,(uint32_t)Right_speed,5,16);          OLED_ShowNum(70,6,(uint32_t)Curr_State,5,16);   
        if (Tishi_flag==1) 
        {
            Tishi_flag=0;
            DL_GPIO_clearPins(TISHI_PORT, TISHI_FMQ_PIN);
            DL_GPIO_clearPins(LED_PORT,LED_PIN_14_PIN);
            delay_ms1(300);
            DL_GPIO_setPins(TISHI_PORT, TISHI_FMQ_PIN);
            DL_GPIO_setPins(LED_PORT,LED_PIN_14_PIN);
        }
    }
}

